package texture;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;

import javax.swing.ImageIcon;
import javax.vecmath.Color3f;

public class ImageTexture extends Texture {

	private int imageHeight, imageWidth;
	private BufferedImage image;

	public ImageTexture() {
		super();
		ImageIcon icon = new ImageIcon("images/flower5.jpg");
		Image i = icon.getImage();
		this.image = new BufferedImage(i.getWidth(null), i.getHeight(null),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = (Graphics2D) this.image.getGraphics();
		g2d.drawImage(i, 0, 0, null);
		this.imageHeight = i.getHeight(null);
		this.imageWidth = i.getWidth(null);
	}

	@Override
	public Color3f getColor(float widthPercent, float depthPercent) {
		int row = (int) (depthPercent * this.imageHeight);
		int col = (int) (widthPercent * this.imageWidth);
		if (row >= imageHeight)
			row = imageHeight;
		if (col >= imageWidth)
			col = imageWidth;
		if (row < 0)
			row = 0;
		if (col < 0)
			col = 0;
		// System.err.println(row + "/" + this.imageHeight + ", " + col + "/"
		// + this.imageWidth);
		try {
			Color c = new Color(this.image.getRGB(col, row));
			return new Color3f(c);
		} catch (ArrayIndexOutOfBoundsException e) {
			return new Color3f(Color.BLACK);
		}
		
	}

}
